//
//  ChouViewController.m
//  WhereToTravel
//
//  Created by dllo on 15/10/16.
//  Copyright © 2015年 zhoushengmin. All rights reserved.
//

#import "ChouViewController.h"
#import "AppDelegate.h"
#import "SetUpViewController.h"
#import "SetUpTableViewCell.h"

@interface ChouViewController ()<UITableViewDataSource,UITableViewDelegate>
{
    NSString *_cleanTitle;
    CGFloat floatSize;
    UILabel *_remo;
}
@property (nonatomic, strong) UIImageView *image;
@property (nonatomic, strong) NSArray *titleArr;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) UIAlertView *alertView;
@property (nonatomic, strong) UIAlertView *alertViewSucceed;

@end

@implementation ChouViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor=[UIColor cyanColor];
    self.image=[[UIImageView alloc]initWithFrame:self.view.frame];
    [self.image setImage:[UIImage imageNamed:@"choubei.jpg"]];
    [self.view addSubview:self.image];
    self.image.userInteractionEnabled = YES;
    [self.image mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.equalTo(self.view);
    }];
    
    self.titleArr = [NSArray arrayWithObjects:@"我的收藏",@"夜间模式",@"清除缓存", nil];
    
    [self createTableView];
    
    self.alertViewSucceed = [[UIAlertView alloc] initWithTitle:@"提示" message:@"清除缓存成功" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
}

#pragma mark tableView的创建
- (void)createTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(180 * WIDTH, HEIGHT * 240, WIDTH * 150, HEIGHT * 300) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.rowHeight = 60 * HEIGHT;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.scrollEnabled = NO;
    [self.view addSubview:self.tableView];
    
}

#pragma mark tableView的数据处理
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *reuse = @"reuse";
    SetUpTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:reuse];
    if (!cell) {
        cell = [[SetUpTableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:reuse];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor clearColor];
    }
    cell.titleLabel.text = self.titleArr[indexPath.row];
    
    // SDImage缓存的图片
    NSString *title = nil;
    if (indexPath.row == 1) {
        NSUInteger size = [SDImageCache sharedImageCache].getSize;
        
        if (size > 1024 * 1024) {
            CGFloat floatSize = size / 1024.0 / 1024.0;
            title = [NSString stringWithFormat:@"%.fM缓存",floatSize];
        }else if (size > 1024){
            CGFloat floatSize = size / 1024.0;
            title = [NSString stringWithFormat:@"%.fKB缓存",floatSize];
        }else if (size > 0){
            title = [NSString stringWithFormat:@"%ldB缓存",size];
        }else{
            title = @"0KB缓存";
        }
        if (indexPath.row == 1) {
            NSString *str = [NSString stringWithFormat:@"您有%@,确定清除缓存?",title];
            self.alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:str delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
            self.alertView.delegate = self;
        }
    }

    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        SetUpViewController *setUpVC = [[SetUpViewController alloc] init];
        UINavigationController *NaVC = [[UINavigationController alloc] initWithRootViewController:setUpVC];
        [self presentViewController:NaVC animated:YES completion:nil];
    } else if (indexPath.row == 1) {
        [self isNight];
    } else if (indexPath.row == 2) {
        [self.tableView reloadData];
        [self clearCache];
    }
}

// 夜间模式
- (void)isNight {
    if ([SingletonNight shareSingletonNight].isSelete == NO) {
        [SingletonNight shareSingletonNight].isSelete = ![SingletonNight shareSingletonNight].isSelete;
        [[NSNotificationCenter defaultCenter] postNotificationName:@"night" object:nil];
    }
    else {
        [SingletonNight shareSingletonNight].isSelete = ![SingletonNight shareSingletonNight].isSelete;
        [[NSNotificationCenter defaultCenter] postNotificationName:@"night" object:nil];
    }
    
    // 将夜间模式保存到本地
    NSString *nightTime = [NSString stringWithFormat:@"%d",[SingletonNight shareSingletonNight].isSelete];
    NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *sandBoxPath = sandBox[0];
    NSString *documentPath = [sandBoxPath stringByAppendingPathComponent:@"nightTime.plist"];
    [NSKeyedArchiver archiveRootObject:nightTime toFile:documentPath];
    NSLog(@"%@",documentPath);
    
}
// 清除缓存
- (void)clearCache {
    [self.alertView show];
}

// 点击确定删除缓存
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == self.alertView.cancelButtonIndex) {
        NSArray *sandBox = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
        NSString *sandBoxPath = sandBox[0];
        // 创建一个文件管理者
        NSFileManager *manager=[NSFileManager defaultManager];
        NSString *filePath=[sandBoxPath stringByAppendingPathComponent:@"缓存"];
        // 通过manager进行文件夹的创建
        [manager createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
        [manager removeItemAtPath:filePath error:nil];
        [self.alertViewSucceed show];
        // 删出缓存图片
        [[SDImageCache sharedImageCache] clearDisk];
        [self.tableView reloadData];
    }
}



//    UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"清除成功" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
//    [alert show];
//    [alert release];






- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
